﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyClassUI.entity;
using EasyClassUI.WebRequest;

namespace EasyClassUI.forms.blog
{
    public partial class BlogAppendice : UserControl
    {
        private string fileName;
        private Post post;
        public BlogAppendice(string appendName, string appendSize, Post post)
        {
            // 初始化文件名以及大小 组件记得改名字
            InitializeComponent();
            string nameToDisplay = appendName;
            string sizeToDisplay = appendSize;

            if (appendName.Length > 6) {
                nameToDisplay = appendName.Substring(0, 5) + "...";
            }

            if (appendSize.Length > 5) {
                sizeToDisplay = appendSize.Substring(0, 4);
            }
            appendiceNameLabel.Text = nameToDisplay;
            appendiceSizeLabel.Text = sizeToDisplay + "KB";
            fileName = appendName;
            this.post = post;
        }

        private void DownloadButton_Click(object sender, EventArgs e) {
            // 帖子附件下载函数
            ResourceService resourceService = new ResourceService();
            string path = string.Empty;
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                path = fbd.SelectedPath;
            }
            resourceService.DownloadFileFromPost(fileName, post.PostID, path + "\\" + fileName);
        }
    }
}
